// 获取应用实例
var app = getApp()
import {
  indexdata,
  appnavapi,
  actclick,
  functionClick,
  noOwningClick
} from "../../utils/api"
import utils from "../../utils/util"

Page({
  data: {
    indexdata: {},
    selectList: [],
    classifyList: [],
    selectIndex: 0
  },

  actionClassify: function (e) {
    var index = e.currentTarget.dataset.index;
    if (this.selectIndex === index) {
      return
    }
    this.setData({
      selectIndex: index,
      selectList: this.classifyList[index].data
    })
  },
  async onLoad() {
    const res = await appnavapi()
    var arr = res.data.data
    if (res.data.code == 200) {
      for (let i = 0; i < arr.foot_navs.length; i++) {
        arr.foot_navs[i].active = true
      }
      app.globalData.tabBar = arr
      this._getindex()
      app.editTabBar()
    } else {
      utils.wxshowtoast("数据请求失败")
    }

  },
  // 首页数据加载
  async _getindex() {
    const res = await indexdata()
    if (res.data.code == 200) {
      for (let datum of res.data.data) {
        if (datum.type === "act") {
          this.classifyList = datum.data
          this.setData({
            selectIndex: 0,
            selectList: datum.data[0].data
          })
        }
      }
      wx.hideLoading()
      this.setData({
        indexdata: res.data.data
      })
    } else {
      wx.hideLoading()
      utils.wxshowtoast("数据请求失败")
      this.setData({
        indexdata: {}
      })
    }
  },
  // 列表详情页的
  indexdetail(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.path,
      fail(error) {
        console.log(error)
      }
    })
  },
  async act_detail(e) {
    var act_data = e.currentTarget.dataset;
    const res = await actclick(act_data.id)
    wx.hideLoading()
    if (act_data.type == 1) {
      wx.navigateTo({
        url: act_data.page,
        fail(error) {
          wx.switchTab({
            url: act_data.page,
            fail(error) {
              utils.wxshowtoast('跳转参数错误')
            }
          })
        }
      })
    } else if (act_data.type == 2) {
      wx.navigateTo({
        url: "/pages/img_preview/img_preview?img=" + act_data.long_h5
      })
    } else if (act_data.type == 3) {
      wx.navigateToMiniProgram({
        appId: act_data.appid,
        path: act_data.path
      })
    } else if (act_data.type == 4) {
      wx.showModal({
        content: act_data.alertmsg,
        success: function (res) {
          return false;
        }
      })
    } else if (act_data.type == 5) {
      console.log(act_data)
      wx.navigateTo({
        url: act_data.pluginprivate
      });
    }
  },
  async act_icon_detail(e) {
    var act_data = e.currentTarget.dataset;
    await functionClick(act_data.id)
    wx.hideLoading()
    if (act_data.type == 1) {
      wx.navigateTo({
        url: act_data.page,
        fail(error) {
          wx.switchTab({
            url: act_data.page,
          })
        }
      })
    } else if (act_data.type == 2) {
      wx.navigateTo({
        url: "/pages/img_preview/img_preview?img=" + act_data.contetimg
      })
    } else if (act_data.type == 3) {
      wx.navigateToMiniProgram({
        appId: act_data.appid,
        path: act_data.path
      })
    } else if (act_data.type == 4) {
      wx.showModal({
        content: act_data.alertmsg,
        success: function (res) {
          return false;
        }
      })
    } else if (act_data.type == 5) {
      wx.navigateTo({
        url: act_data.plugin_private
      });
    }
  },
  async onRefresh() {
    //导航条加载动画
    wx.showNavigationBarLoading()
    const res = await indexdata()
    if (res.data.code == 200) {
      wx.hideNavigationBarLoading();
      wx.hideLoading()
      //停止下拉刷新
      wx.stopPullDownRefresh();
      this.setData({
        indexdata: res.data.data
      })
    } else {
      utils.wxshowtoast("数据刷新失败")
      wx.hideLoading()
      this.setData({
        indexdata: {}
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },
  // 详情页面跳转判断
  detail(e) {
    if (e.currentTarget.dataset.owning === 1) {
      wx.navigateTo({
        url: "/pages/detail/detail?id=" + e.currentTarget.dataset.id
      })
    } else {
      noOwningClick()
      wx.navigateToMiniProgram({
        appId: 'wx32540bd863b27570',
        path: e.currentTarget.dataset.path

      })
    }

  },
  // 查看更多页面跳转
  indexmuch(e) {
    wx.navigateTo({
      url: "/pages/indexdetail/indexmuch/indexmuch?class_id=" + JSON.stringify(e.currentTarget.dataset.classid)
    })
  },
  // 页面的跳转banner和魔方的
  urlfn(e) {
    if (wx.getStorageSync('openid') == "") {
      wx.navigateTo({
        url: "/pages/login/login"
      })
      return
    }
    if (e.currentTarget.dataset.url == '') {
      return
    }
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
      fail(error) {
        wx.switchTab({
          url: act_data.page,
        })
      }
    })
  },
  onShow() {
    this.onLoad()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //销毁定时器

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  searchFun: function () {
    wx.showModal({
      title: '提示',
      content: '是否跳转到三方平台',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          wx.navigateToMiniProgram({
            appId: 'wx32540bd863b27570',
            path: 'package_a/search_result/search_result?name=1111'

          })
          console.log('用户点击确定')
        } else { //这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  }
})
